import Layout from "@/components/layout";
import Vue from "vue";
import VueRouter from "vue-router";

Vue.use(VueRouter);
/**
 * 对当前路径跳转的报错进行捕获,不抛出错误
 */
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err);
};
const routes = [
  {
    path: "/",
    redirect: {
      name: "CropHospital",
    },
    component: Layout,
    children: [
      {
        path: "crop-hospital",
        name: "CropHospital",
        meta: {
          middle: {
            type: "title",
            text: "甘蔗医院",
          },
        },
        component: () =>
          import(
            /* webpackChunkName: "CropHospital" */ "../views/crop-hospital"
          ),
      },
      {
        path: "cropdoctor-list",
        name: "CropdoctorList",
        meta: {
          left: {
            type: "nav",
            text: "返回",
          },
          middle: {
            type: "search", // 如果是search  支持 传入placeholder
            placeholder: "请输入查询内容",
          },
          right: {
            type: "searchTitile",
            text: "搜索",
          },
        },
        component: () =>
          import(
            /* webpackChunkName: "CropdoctorList" */ "../views/cropdoctor-list"
          ),
      },
      {
        path: "problem-list",
        name: "ProblemList",
        meta: {
          left: {
            type: "nav",
            text: "返回",
          },
          middle: {
            type: "search", // 如果是search  支持 传入placeholder
            placeholder: "请输入查询内容",
            searchWidth: "233px",
          },
          right: {
            type: "searchTitile",
            text: "搜索",
          },
        },
        component: () =>
          import(/* webpackChunkName: "ProblemList" */ "../views/problem-list"),
      },

      {
        path: "service/:type/:id",
        name: "ServiceForm",
        meta: {
          border: true,
          key: "type",
          left: {
            type: "nav",
          },
          middle: {
            type: "title",
            text(type) {
              const titleMap = {
                0: "在线问诊",
                1: "预约挂号",
                2: "上门服务",
              };
              return titleMap[type];
            },
          },
        },
        props: true,
        component: () =>
          import(/* webpackChunkName: "ServiceForm" */ "../views/service-form"),
      },
      {
        path: "service-detail/:type/:id",
        name: "ServiceDetail",
        meta: {
          border: true,
          key: "type",
          left: {
            type: "nav",
          },
          middle: {
            type: "title",
            text(type) {
              const titleMap = {
                0: "问诊详情",
                1: "挂号详情",
                2: "服务详情",
              };
              return titleMap[type] || "非法访问";
            },
          },
        },
        props: true,
        component: () =>
          import(
            /* webpackChunkName: "ServiceDetail" */ "../views/service-detail"
          ),
      },
      {
        path: "agricultural-class",
        name: "AgriculturalClass",
        meta: {
          border: true,
          middle: {
            type: "title",
            text: "农技课堂",
          },
        },
        component: () =>
          import(
            /* webpackChunkName: "AgriculturalClass" */ "../views/agricultural-class"
          ),
      },
      {
        path: "online-reply",
        name: "OnlineReply",
        meta: {
          border: true,
          left: {
            type: "nav",
          },
          middle: {
            type: "title",
            text: "在线答疑",
          },
        },
        component: () =>
          import(/* webpackChunkName: "OnlineReply" */ "../views/online-reply"),
      },
      {
        path: "reply-form/:role/:id",
        name: "ReplyForm",
        props: true,
        meta: {
          border: true,
          key: "role",
          left: {
            type: "nav",
          },
          middle: {
            type: "title",
            text(role) {
              const titleMap = {
                expert: "解答",
                farmer: "提问",
              };
              return titleMap[role];
            },
          },
        },
        component: () =>
          import(/* webpackChunkName: "ReplyForm" */ "../views/reply-form"),
      },
      {
        path: "online-ask",
        name: "OnlineAsk",
        meta: {
          left: {
            type: "nav",
            text: "返回",
          },
          middle: {
            type: "search",
            placeholder: "请输入查询内容",
          },
          right: {
            type: "link",
            text: "提问",
            linkname: "ReplyForm",
            linkparams: {
              role: "farmer",
              id: "commonId",
            },
          },
        },
        component: () =>
          import(/* webpackChunkName: "OnlineAsk" */ "../views/online-ask"),
      },
      {
        path: "ask-reply-detail/:role/:id",
        name: "AskReplyDetail",
        props: true,
        meta: {
          border: true,
          left: {
            type: "nav",
          },
          middle: {
            type: "title",
            text: "问答详情",
          },
        },
        component: () =>
          import(
            /* webpackChunkName: "AskReplyDetail" */ "../views/ask-reply-detail"
          ),
      },
      {
        path: "expert-video-list",
        name: "ExpertVideoList",
        meta: {
          left: {
            type: "nav",
            text: "返回",
          },
          middle: {
            type: "search",
            placeholder: "请输入查询内容",
          },
          right: {
            type: "link",
            text: "发布",
            linkname: "PublishVideo",
            linkparams: {
              type: "add",
              id: "commonId",
            },
          },
        },
        component: () =>
          import(
            /* webpackChunkName: "ExpertVideoList" */ "../views/expert-video-list"
          ),
      },
      {
        path: "publish-video/:type/:id",
        name: "PublishVideo",
        meta: {
          border: true,
          key: "type",
          left: {
            type: "nav",
          },
          middle: {
            type: "title",
            text(type) {
              const typeMap = {
                add: "课程发布",
                edit: "编辑课程",
              };
              return typeMap[type];
            },
          },
        },
        component: () =>
          import(
            /* webpackChunkName: "PublishVideo" */ "../views/publish-video"
          ),
      },
      {
        path: "farmer-video-list",
        name: "FarmerVideoList",
        meta: {
          left: {
            type: "nav",
            text: "返回",
          },
          middle: {
            type: "search",
            placeholder: "请输入查询内容",
          },
          right: {
            type: "searchTitile",
            text: "搜索",
          },
        },
        component: () =>
          import(
            /* webpackChunkName: "FarmerVideoList" */ "../views/farmer-video-list"
          ),
      },
      {
        path: "video-course/:courseId",
        name: "VideoCourse",
        meta: {
          left: {
            type: "nav",
          },
        },
        component: () =>
          import(/* webpackChunkName: "VideoCourse" */ "../views/video-course"),
      },
      {
        path: "video-ecaluate/:courseId/:type",
        name: "VideoEcaluate",
        meta: {
          border: true,
          left: {
            type: "nav",
          },
          middle: {
            type: "title",
            text: "全部评价",
          },
        },
        component: () =>
          import(
            /* webpackChunkName: "VideoEcaluate" */ "../views/video-ecaluate"
          ),
      },
      {
        path: "video-payment/:id",
        name: "VideoPayment",
        meta: {
          border: true,
          left: {
            type: "nav",
          },
          middle: {
            type: "title",
            text: "收银台",
          },
        },
        component: () =>
          import(
            /* webpackChunkName: "VideoEcaluate" */ "../views/video-payment"
          ),
      },
      {
        path: "video-reviewer/:courseId",
        name: "VideoReviewer",
        meta: {
          left: {
            type: "nav",
          },
          middle: {
            type: "title",
            text: "",
          },
        },
        component: () =>
          import(
            /* webpackChunkName: "VideoEcaluate" */ "../views/video-reviewer"
          ),
      },
      {
        path: "my-service",
        name: "myService",
        meta: {
          border: false,
          left: {
            type: "nav",
          },
          middle: {
            type: "title",
            text: "我的服务",
          },
        },
        component: () => import("../views/my-service"),
      },
      {
        path: "my-service/evaluation/:type/:id",
        name: "evaluation",
        meta: {
          border: true,
          left: {
            type: "nav",
          },
          middle: {
            type: "title",
            text: "发表评价",
          },
        },
        component: () => import("../views/my-service/evaluation"),
      },
      {
        path: "my-service/seeEvaluation/:type/:id",
        name: "seeEvaluation",
        meta: {
          border: true,
          left: {
            type: "nav",
          },
          middle: {
            type: "title",
            text: "查看评价",
          },
        },
        component: () => import("../views/my-service/seeEvaluation"),
      },
      {
        path: "agri-news",
        name: "agriNews",
        meta: {
          left: {
            type: "nav",
          },
          middle: {
            type: "title",
            text: "农情动态",
          },
        },
        component: () => import("../views/agri-news"),
      },
      {
        path: "Experts-in/:flag",
        name: "ExpertsIn",
        meta: {
          border: true,
          left: {
            type: "nav",
          },
          middle: {
            type: "title",
            text: "完善信息",
          },
        },
        component: () => import("../views/Experts-in"),
      },
      {
        path: "submitSuccess",
        name: "submitSuccess",
        meta: {
          border: true,
          left: {
            type: "nav",
          },
          middle: {
            type: "title",
            text: "审核详情",
          },
        },
        component: () => import("../views/Experts-in/submitSuccess.vue"),
      },
      // {
      // 	path: 'auditResult/:type',
      // 	name: 'auditResult',
      // 	meta: {
      // 		border: true,
      // 		left: {
      // 			type: 'nav',
      // 		},
      // 		middle: {
      // 			type: 'title',
      // 			text: '完成',
      // 		},
      // 	},
      // 	component: () => import( '../views/Experts-in/auditResult.vue'),
      // },
      {
        path: "agri-news/:id",
        name: "agriNewsDetail",
        meta: {
          border: true,
          left: {
            type: "nav",
          },
          middle: {
            type: "title",
            text: "资讯详情",
          },
        },
        component: () => import("../views/agri-news/news-detail"),
      },
    ],
  },
];

const nameArray = [];
const urlArray = [];
const list = routes[0].children;
list.forEach(async (item, index) => {
  nameArray.push(item.name);
  urlArray.push(item.path);
  if (list[index + 1] && nameArray.includes(list[index + 1].name)) {
    alert(`name重复,请修改name,重复的name====>${list[index + 1].name}`);
  }
  if (list[index + 1] && urlArray.includes(list[index + 1].path)) {
    alert(
      `path重复,请修改path或复查看是否已有相关路由配置,重复的path====>${
        list[index + 1].path
      }`
    );
  }
});
const router = new VueRouter({
  routes,
});
export default router;
